package com.ruoyi.anan.util;

import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author anan.
 * @version 1.0
 * @create 2021-04-14 13-48
 */
public class ZhihesjUtil {

    /**
     * 将下滑线的list转换成驼峰list
     * @param list
     * @return
     */
    public static List<Map<String,Object>> getConResults(List list) {
        if (list.size() > 0) {
            Map keysMap = getObjectKeyList(list);
            return getConResults(list, keysMap);
        }
        return list;
    }

    /**
     * 将list的key值提取出来
     * @param list
     * @return
     */
    private static Map getObjectKeyList(List list) {
        if (list.size() > 0) {
            Map<String, Object> objectMap = (Map<String, Object>) list.get(0);
            Set<String> keySet = objectMap.keySet();
            List<String> list2 = new ArrayList<String>(keySet);
            List<String> list1 = new ArrayList<String>();
            Iterator<String> it = keySet.iterator();
            while (it.hasNext()) {
                list1.add(replaceUnderline(it.next().toLowerCase(), "_", ""));
            }
            Map map = new HashMap(16);
            map.put("listOld", list2);
            map.put("listNew", list1);
            return map;
        }
        return null;
    }

    /**
     * 将下滑线的list转换成驼峰list
     * @param list
     * @param keysMap
     * @return
     */
    private static List<Map<String,Object>> getConResults(List list, Map keysMap) {
        List resultList = new ArrayList();
        List listOld = (List) keysMap.get("listOld");
        List listNew = (List) keysMap.get("listNew");
        for (int i = 0; i < list.size(); i++) {
            Map map = new HashMap(16);
            String td = "";
            for (int j = 0; j < listOld.size(); j++) {
                if ((((Map<String, Object>) list.get(i)).get(listOld.get(j))) == null) {
                    td=null;
                } else {
                    td = (((Map<String, Object>) list.get(i)).get(listOld.get(j))).toString();
                }
                map.put(listNew.get(j), td);
            }
            resultList.add(map);
        }
        return resultList;
    }



    /**
     * 替换字符串并让它的下一个字母为大写  //驼峰命名
     * @param srcStr
     * @param org
     * @param ob
     * @return  System.out.println(replaceUnderlineAndfirstToUpper("ni_hao_abc","_",""));
     */
    private static String replaceUnderline(String srcStr,String org,String ob)
    {
        String newString = "";
        int first = 0;
        while (srcStr.indexOf(org) != -1) {
            first = srcStr.indexOf(org);
            if (first != srcStr.length()) {
                newString = newString + srcStr.substring(0, first) + ob;
                srcStr = srcStr.substring(first + org.length(), srcStr.length());
                srcStr = firstCharacterToUpper(srcStr);
            }
        }
        newString = newString + srcStr;
        return newString;
    }

    /**
     * 首字母大写
     *
     * @param srcStr
     * @return
     */
    private static String firstCharacterToUpper(String srcStr) {
        return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);
    }
}
